--- interact_link: content/08/01/plotly8-1.ipynb kernel_name: python3 kernel_path: content/08/01 has_widgets: false title: |- Plotly Graph Options pagenum: 14 prev_page: url: /08/Plotly8.html next_page: url: /08/02/plotly8-2.html suffix: .ipynb search: layout title dict method bar size figures plot chart font fig plotly graph figure below updating calling updatelayout color royalblue options data etc update set same updatetraces id objects graphics following manner graphobjects specify e example want colors above dictionary text marker selector mediumpurple markercolor plotlygraphoptions provides hierarchy classes called used construct several benefits compared plain dictionaries imported import plotlygraphoptionsframework framework value top level visual after class agruement optionsbar scatter contour where properties apply whole customizing items axes annotations shapes legend structure object chances create youll customize own titles themes accomplished ly python creating equivalent perform task only titletext titlefont comment: "***PROGRAMMATICALLY GENERATED, DO NOT EDIT. SEE ORIGINAL FILES IN /content***" ---
Plotly Graph Options

Plotly Graph Options

Plotly provides a hierarchy of classes called "graph objects" that may be used to construct figures. Graph objects have several benefits compared to plain dictionaries.

Graphics options can be imported in the following manner:

import plotly.graph_objects as go

Framework of plotly.graph_objects

The value of the top-level data is the data you would like to plot. You will specify your visual as a method after the go class in the data agruement (i.e. go.Bar for a bar chart, but there many options;"bar", "scatter", "contour", etc.)

layout is where you will specify the properties of the figure's layout. The layout options can apply to the figure as a whole, customizing items like the axes, annotations, shapes, legend, and more.

Below is an example of the structure of how to use graph object:

import plotly.offline as pyo
import plotly.graph_objects as go
from plotly.offline import init_notebook_mode, plot
from IPython.core.display import display, HTML
init_notebook_mode(connected=True)

fig = go.Figure(
    data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])],
)
display(HTML('figure8-1.html'))

Updating Figures

Chances are when you create a plot you'll want to customize it with your own colors, titles, themes, etc. All of this can be accomplished by calling the update_layout method on your figure.

Below is an example of how to update the above chart with a title:

fig = go.Figure(
    data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])]
)
fig.update_layout(title_text="This is a Title",
                  title_font_size=30) #Setting a title with 30 font

display(HTML('figure8-2.html'))

Below are all equivalent way to perform the above task:

  • Calling only font size in a dictionary:

    fig.update_layout(title_text="A Bar Chart", title_font=dict(size=30))

  • Calling both title name & font size in a dictionary:

    fig.update_layout(title=dict(text="A Bar Chart"), font=dict(size=30))

  • Calling the Title method from layout

    fig.update_layout(title=go.layout.Title(text="A Bar Chart", font=go.layout.title.Font(size=30)))

You can update the styles of your graphics (i.e. lines, points, etc.) inside of the Figure or add_trace methods.

This is done in the following manner:

go.Figure(y=[4, 2, 3.5], mode="markers", marker=dict(size=20, color="LightSeaGreen"))

However, if you want to set the same style across multiple figures in the same plot it can get very messy to write the same code many times. To aviod this you can call the update_traces method. This will allow you to set universial styling rules for your plot.

Below are a few examples of using the method:

  • Updating the color to RoyalBlue for all bars in the view:

    fig.update_traces(marker=dict(color="RoyalBlue"),selector=dict(type="bar"))

  • Updating the color to RoyalBlue for all colors that are currently set as MediumPurple:

    fig.update_traces(marker_color="RoyalBlue", selector=dict(marker_color="MediumPurple"))